﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZhiAo.WebLib.Data;
using ZhiAo.CommonLibrary;

public partial class ShuiWenJu_RepRealTime_GetXml : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadPam();
        LoadXml();
    }

    private void LoadPam()
    {
        string pram = PageHelper.Request("pram");
        string[] arrPram = pram.Split(',');
        if (arrPram.Length > 0)
            this.Type = arrPram[0];
        if (arrPram.Length > 1)
            this.SiteId = arrPram[1];
        if (arrPram.Length > 2)
            this.TM = arrPram[2];
        if (this.TM == "")
        {
            if (DateTime.Now.Hour > 8)
            {
                this.TM = DateTime.Today.ToString("yyyy-MM-dd");
            }
            else
            {
                this.TM = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
            }
        }

    }

    private void LoadXml()
    {
        #region
        string xml = "";
        xml += "<graph caption='' subcaption='' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='2' decimalPrecision='2' showvalues='2' numdivlines='3' numVdivlines='0' yaxisminvalue='1000' yaxismaxvalue='10'  rotateNames='2' lineThickness='1' anchorAlpha='0'>";
        if (this.Type.Trim() == "A0012")
        {
            xml += GetColumn2D();
        }
        else
        {
            xml += GetLine();
        }

        xml += "</graph>";

        Response.Write(xml);
        Response.End();
        #endregion
    }

    private string GetLine()
    {
        string xml = "";
        ArrayList ht = new ArrayList();
        DataTable dt = GetData();
        int a = 0;
        foreach (DataRow dr in dt.Rows)
        {

            string tm = dr["TM"].ToString();

            if (int.Parse(tm.Substring(11, 2)) % 2 == 0 && tm.Substring(14, 2) == "00")
            {
                xml += "<set name='" + tm.Substring(11, 5) + "' value='" + dr["DTRN"].ToString() + "'  />";
            }
            else
                xml += "<set name='" + tm.Substring(11, 5) + "' value='" + dr["DTRN"].ToString() + "' showName='0' />";    
            a++;
        }
        for (int i = a; i < 288; i++)
        {
            int h = (i / 12 + 7) % 24 + 1;
            int m = i % 12 * 5;
            if (m == 0 && h%2 == 0)
            {
                xml += "<set name='" + h.ToString("d2") + ":00' />";
            }
            else
                xml += "<set name='" + h.ToString("d2") + ":" + m.ToString("d2") + "' showName='0' />";
        }
        return xml;
    }

    private string GetMsLine()
    {
        string xml = "";
        xml += "<categories>";
        DateTime currTm = DateTime.Now;
        #region head
        ArrayList ht = new ArrayList();
        for (int x = 8; x < 24; x++)
        {
            if (x % 2 == 0)
            {
                xml += "<category name='" + x.ToString() + ":00" + "' />";
                ht.Add(this.TM + " " + (x < 10 ? "0" + x.ToString() : x.ToString()) + ":00:00");
            }

        }
        for (int x = 0; x <= 8; x++)
        {
            if (x % 2 == 0)
            {
                xml += "<category name='" + x.ToString() + ":00" + "' />";
                if (!ht.Contains(this.TM + " " + (x < 10 ? "0" + x.ToString() : x.ToString()) + ":00:00"))
                    ht.Add(this.TM + " " + (x < 10 ? "0" + x.ToString() : x.ToString()) + ":00:00");
            }
        }

        xml += "</categories>";
        #endregion


        DataTable dt = GetData();
        Hashtable htDetail = new Hashtable();
        foreach (DataRow dr in dt.Rows)
        {

            string tm = dr["TM"].ToString();
            if (ht.Contains(tm))
            {
                htDetail.Add(tm, dr["DTRN"].ToString());
            }
        }

        xml += "<dataset seriesName='' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
        for (int a = 0; a < ht.Count; a++)
        {

            if (htDetail.Contains(ht[a]))
            {
                xml += "<set value='" + htDetail[ht[a]] + "' />";
            }
            else
            {
                xml += "<set value='' />";
            }
        }
        xml += "</dataset>";
        return xml;
    }


    private string GetColumn2D()
    {
        string xml = "";
        #region head
        ArrayList ht = new ArrayList();
        for (int x = 8; x < 24; x++)
        {
            if (x % 2 == 0)
            {
                ht.Add(this.TM + " " + (x < 10 ? "0" + x.ToString() : x.ToString()) + ":00:00");
            }

        }
        for (int x = 0; x <= 8; x++)
        {
            if (x % 2 == 0)
            {
                if (!ht.Contains(this.TM + " " + (x < 10 ? "0" + x.ToString() : x.ToString()) + ":00:00"))
                    ht.Add(this.TM + " " + (x < 10 ? "0" + x.ToString() : x.ToString()) + ":00:00");
            }
        }
        #endregion

        DataTable dt = GetData();
        Hashtable htDetail = new Hashtable();
        foreach (DataRow dr in dt.Rows)
        {

            string tm = dr["TM"].ToString();
            if (ht.Contains(tm))
            {
                htDetail.Add(tm, dr["DTRN"].ToString());
            }

        }

        for (int a = 0; a < ht.Count; a++)
        {
            DateTime tm = DateTime.Parse(ht[a].ToString());
            if (htDetail.Contains(ht[a]))
            {
                xml += "<set name='" + tm.Hour.ToString() + ":00" + "' value='" + htDetail[ht[a]] + "' color='AFD8F8' />";
            }
            else
            {
                xml += "<set name='" + tm.Hour.ToString() + ":00" + "' value='" + htDetail[ht[a]] + "' color='AFD8F8' />";
            }
        }
        return xml;
    }

    private string Type
    {
        get { return ViewState["Type"] == null ? "" : ViewState["Type"].ToString(); }
        set { ViewState["Type"] = value; }
    }
    private string SiteId
    {
        get { return ViewState["SiteId"] == null ? "" : ViewState["SiteId"].ToString(); }
        set { ViewState["SiteId"] = value; }
    }
    private string TM
    {
        get { return ViewState["TM"] == null ? "" : ViewState["TM"].ToString(); }
        set { ViewState["TM"] = value; }
    }

    private DataTable GetData()
    {
        DataTable dt = new DataTable();
        //string siteId = Session["currentSiteId"] as string;
        string sql = "";
        sql += "EXEC [dbo].[XmlData]";
        sql += "@TYPE = N'{0}',";
        sql += "@SITEID = N'{1}',";
        sql += "@CURRDATE = N'{2}'";
        sql = string.Format(sql, this.Type, this.SiteId, this.TM + " 08:00:00");//this.SiteId"1071"
        DataSet ds = DBHelper.GetDataSet(sql);
        if (ds.Tables.Count > 0)
            dt = ds.Tables[0];
        return dt;
    }





    private void test()
    {
        #region
        string xml = "";
        xml += "<graph caption='' subcaption='' hovercapbg='FFECAA' hovercapborder='F47E00' formatNumberScale='0' decimalPrecision='0' showvalues='0' numdivlines='3' numVdivlines='0' yaxisminvalue='1000' yaxismaxvalue='50'  rotateNames='1'>";
        xml += "<categories>";
        DateTime currTm = DateTime.Now;
        for (DateTime tm = currTm.AddMonths(-6); tm < currTm; tm = tm.AddMonths(1))
        {
            xml += "<category name='" + tm.ToString("yy/MM/dd") + "' />";
        }
        xml += "</categories>";

        DataTable dt = GetData();
        string F = "";
        string TYPENAME = "";
        string DTRN = "";
        string TM = "";
        string[] arrColor = new string[] { "1D8BD1", "F1683C" };
        int a = 0;
        int m = 0;
        foreach (DataRow dr in dt.Rows)
        {
            TYPENAME = dr["TYPENAME"].ToString();
            DTRN = dr["DTRN"].ToString();
            TM = dr["TM"].ToString();
            if (TYPENAME != F && a != 0)
            {
                xml += "</dataset>";
            }
            if (a == 0 || TYPENAME != F)
            {
                xml += "<dataset seriesName='{0}' color='{1}' anchorBorderColor='{1}' anchorBgColor='{1}'>";
                xml = string.Format(xml, TYPENAME, arrColor[m]);
                m++;
            }
            xml += "<set value='" + DTRN + "' />";
            if (a == dt.Rows.Count - 1)
            {
                xml += "</dataset>";
            }
            F = TYPENAME;
            a++;
        }
        xml += "</graph>";

        Response.Write(xml);
        Response.End();
        #endregion
        #region
        //string type = Request.QueryString["type"];
        //string sql = "";
        //switch (type.ToUpper())
        //{
        //    case "A0011"://氯度------------1
        //        sql += "SELECT '氯度' AS TYPENAME,";
        //        sql += "       SUM(A.ppm) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.TM, 120), 1, 13) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.TM >= '{1}' AND A.TM < DATEADD(dd, 1, '{1}')";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.TM, 120), 1, 13)";
        //        break;
        //    case "A0015"://蒸发------------2
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";
        //        break;
        //    case "A0012"://雨量------------3
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       A.YMDHM TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= '{1}' AND A.YMDHM < DATEADD(dd, 1, '{1}')";
        //        sql += " GROUP BY A.YMDHM ";
        //        break;
        //    case "A0013"://水位------------4
        //        sql += " SELECT '水位' AS TYPENAME,";
        //        sql += "       SUM(A.ZU) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_DAM_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";

        //        break;
        //    case "A00141"://气压------------5
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";
        //        break;
        //    case "A00143"://风速------------6
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";
        //        break;
        //    case "A00142"://温度------------7
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";
        //        break;
        //    case "A00144"://风向------------8
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";
        //        break;
        //    case "A00145"://湿度------------9
        //        sql += "SELECT '雨量' AS TYPENAME,";
        //        sql += "       SUM(A.DTRN) AS DTRN,";
        //        sql += "       SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10) AS TM";
        //        sql += "  FROM ST_RNFL_R A";
        //        sql += " WHERE A.STCDT = '{0}'";
        //        sql += "  AND A.YMDHM >= DATEADD(MM, -600, GETDATE())";
        //        sql += " GROUP BY SUBSTRING(CONVERT(varchar, A.YMDHM, 120), 1, 10)";
        //        break;
        //    default: break;

        //}
        #endregion
    }
}
